<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Blog Model
*
* Author:  William Bermudo
*
*/

class Blog_m extends DataMapperExt
{
	var $table = 'posts';
	
	var $has_one = array(
						'user' => array(
							'class'			=> 'users_m',
							'other_field' 	=> 'blogs',
							'join_other_as'	=> 'author',
							'join_table' 	=> 'posts'
						),
						'category'	=> array(
							'class'			=> 'blog_categories_m',
							'other_field'	=> 'blogs',
							'join_other_as'	=> 'category',
							'join_table'	=> 'posts'
						)
					);
	
	public function __construct($id = NULL)
	{
		parent::__construct($id);
	}
	
	public function get_posts_by_url($year, $month, $day, $url_title)
	{
		$date = $year.'-'.$month.'-'.$day;
		
		$post = new Blog_m();
			
		$post->include_related('user', array('id','username'), TRUE, TRUE)
							->where('status', 'live')
							->where('url_title', $url_title)
							->where('date_posted', $date)
							->limit(1)
							->get();
		
		if ($post->count() == 1)
		{
			$categories = new Blog_categories_m();
			
			$categories->include_related('blogs')->where('category', $post->id)->get();
		}
		
		$categories = new Blog_categories_m();
		
		$categories->include_related('blogs')->get();
		
		foreach($categories as $c)
		{
			$c->blogs->include_related('user',array('id','username'),TRUE,TRUE)->get();
			echo $c->title . ' : <br>';
			foreach($c->blogs as $cb)
			{
				echo '&nbsp;&nbsp;&nbsp;'.$cb->title .' Written By : '.$cb->user->username.'<br/>';
			}
		}
	}
	
	public function get_post_by_id($user_id = NULL)
	{
		$categories = new Blog_categories_m();
		
		$categories->include_related('blogs')->where('author_id', $user_id)->get();
		
		foreach($categories as $category)
		{
			$category->blogs->include_related('user',array('id','username'),TRUE,TRUE)->get();
			echo $category->title . ' : <br>';
			foreach($category->blogs as $cb)
			{
				echo '&nbsp;&nbsp;&nbsp;'.$cb->title .' Written By : '.$cb->user->username.'<br/>';
			}
		}
	}
	
	public function get_user_posts_categories()
	{
		echo 'BY Post : <br/>';
		$post = new Blog_m();
		
		$post
			->include_related('user',array('id','username'),TRUE,TRUE)
			->include_related('category',array('id','title'),TRUE,TRUE)
			->get();
			
		foreach($post as $p)
		{
			echo $p->user->username. ' Wrote: '.$p->title.' Category : '.$p->category->title.'<br/>';
		}
		
		echo '<br/><br/><br/> BY User: <br/>';
		
		$user = new Users_m();
		$user->get();
		
		foreach($user as $u)
		{
			echo $u->username .' : <br/>';
			$u->blogs->include_related('category',array('id','title'),TRUE,TRUE)->get();
			foreach($u->blogs as $ub)
			{
				echo '&nbsp;&nbsp;&nbsp;'.$ub->title.' category : '.$ub->category->title.'<br/>';
			}
			
		}
		
		
		echo '<br/><br/><br/> BY Catgories: <br/>';
		
		$categories = new Blog_categories_m();
		
		$categories->include_related('blogs')->get();
		
		foreach($categories as $c)
		{
			$c->blogs->include_related('user',array('id','username'),TRUE,TRUE)->get();
			echo $c->title . ' : <br>';
			foreach($c->blogs as $cb)
			{
				echo '&nbsp;&nbsp;&nbsp;'.$cb->title .' Written By : '.$cb->user->username.'<br/>';
			}
		}
		
	}
	
		//public function get_user_posts()
		//{
		//	$post = new Blog_m();
		//	
		//	$post->include_related('user', array('id','username'), TRUE, TRUE)->get();
		//			
		//	foreach($post as $p)
		//	{
		//		echo $p->user->username. ' Wrote: '.$p->title.'<br/>';
		//	}
		//
		//}
}
	